﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Schedulingclasses.Api.DTO;
using Schedulingclasses.Api.User.Command;
using Schedulingclasses.Domain;
using Schedulingclasses.ErrorCode;

namespace Schedulingclasses.Api.Controllers
{
    /// <summary>
    /// 班级排课管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
   
    public class ClassesController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ClassesController> logger;

        public ClassesController(IMediator mediator,ILogger<ClassesController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<ApiResult<LoginDto>> Handle([FromQuery]LoginCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 添加流程信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        [Authorize]
        public async Task<ApiResult<int>> CreateInstncce(CreateInstncceComandd request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 根据角色获取对应信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiPaging<instancce>> SraechInstancce([FromQuery]SraechInstancceCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 审批处理
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPut]
        [Authorize]
        public async Task<ApiResult<int>> UpdateInstance(UpdateInstanceCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 获取级联数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult<List<SreachRegionDto>>> SreachRegion([FromQuery]SreachRegionCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
    }
}
